//
//  SocketViewController.m
//  IPTV
//
//  Created by liao Echo on 14-5-9.
//  Copyright (c) 2014年 Echo. All rights reserved.
//

#import "SocketViewController.h"
#import "PokerControlClient.h"
#import "RemoteControlClient.h"
#import "CreateTaskViewController.h"

@interface SocketViewController ()

@end

@implementation SocketViewController
@synthesize m_savaDevice;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    TableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-44-20) style:UITableViewStylePlain];
    TableView1.backgroundColor = [UIColor clearColor];
    TableView1.delegate = self;
    TableView1.dataSource = self;
    TableView1.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:TableView1];
    
    [[PokerControlClient share] c2sPing];
    
    if ([PokerControlClient share].isRemote) {
        [[PokerControlClient share] startCmd:@"100" code:nil];//获取设备是否在线
    }
    
    [NC addObserver:self selector:@selector(switchInfo:) name:NOTI_SWITCH_INFO object:nil];
    
    GoodBtn *btn = [GoodBtn btnWithImg:@"timing_delay_bg.png"];
    btn.frame = CGRectMake((SCREEN_WIDTH-45)/2, FH(self.view)-60-60, 45, 45);
    [btn addTarget:self action:@selector(newViewPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}
-(void)newViewPressed{
    CreateTaskViewController *view = [[CreateTaskViewController alloc] init];
    view.m_savaDevice = self.m_savaDevice;
    [self.navigationController pushViewController:view animated:YES];
}
-(void)switchInfo:(NSNotification*)nofi{
    on_off = [nofi.object intValue];
    [TableView1 reloadData];
    [[PokerControlClient share] stopc2sPing];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TableView1Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 62)];
    bg.backgroundColor = [UIColor whiteColor];
    [cell addSubview:bg];
    
    UIImageView *logo = [[UIImageView alloc] initWithImage:IMG(@"icon_socket.png")];
    logo.frame = CGRectMake(SCREEN_WIDTH-1.5-8-8-55/1.8, 7, 55/1.8, 84/1.8);
    [cell addSubview:logo];
    
    UISwitch *switchBtn = [[UISwitch alloc] init];
    switchBtn.tag = indexPath.row;
    switchBtn.frame = CGRectMake(1.5+8, 15, 66-8*2, 43);
    [cell addSubview:switchBtn];
    if (on_off) {
        [switchBtn setOn:YES];
    }
    [switchBtn addTarget:self action:@selector(onSwitchValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    GoodLabel *nameLabel = [GoodLabel labelWithFont:20 title:STR(@"")];
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.frame = CGRectMake(85, 19, kScreenWidth/2, 25);
    nameLabel.textAlignment = UITextAlignmentCenter;
    nameLabel.text = STR(@"Wi-Fi智能插座");
    if ([Global curLanguageEn]) {
        nameLabel.text = @"Smart Socket";
    }
    [cell addSubview:nameLabel];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
-(IBAction)onSwitchValueChanged:(UISwitch*)sender{
    if ([PokerControlClient share].isRemote) {
        if (sender.on) {
            [[PokerControlClient share] startOnOff:@"13" code:@"1"];
        }else{
            [[PokerControlClient share] startOnOff:@"13" code:@"0"];
        }
    }else{
        if (sender.on) {
            [[PokerControlClient share] startOnOff:@"13" code:@"1"];
        }else{
            [[PokerControlClient share] startOnOff:@"13" code:@"0"];
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
